package cn.gok.homework;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class Homework {
    public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
        //获取类信息();
//        获取书的实例();
        破坏封装();
    }

    private static void 破坏封装() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        Class c = Book.class;
        Book book = new Book(1,"aaa",500f);
        Method m = c.getDeclaredMethod("sell",null);
        m.setAccessible(true);
        m.invoke(book);

        Method m1 = c.getDeclaredMethod("sell",float.class,String.class);
        m1.setAccessible(true);
        m1.invoke(book,500f,"abc");
    }

    private static void 获取书的实例() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class c = Class.forName("cn.gok.homework.Book");
        Constructor con = c.getConstructor(int.class,String.class,float.class);
        Book book = (Book) con.newInstance("aaa","aaa",100f);
        System.out.println(book);
    }

    private static void 获取类信息() {
        Class c = Book.class;
        for (Method m:c.getDeclaredMethods()){
            System.out.println( m.getName()+",参数个数:"
                    +m.getParameterCount()+",返回值类型:"+m.getReturnType().getName()+",可访问性:"+m.getModifiers());
        }
        for(Constructor con:c.getDeclaredConstructors()){
            System.out.println(con);
        }
    }
}
